package com.cjzuji.rental.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class RenRZuBillUtil {
//    public static List<Map<String, Object>> getRenRZuBillList(String authorization,String orderId,String  cookie) {
//        List<Map<String, Object>> result = new ArrayList<>();
//        List<String> headerList = null;
//        int totalTrems = 0;
//        try {
//            String url1 = "https://admin.rrzu.com/v2-bill/index?order_id="+orderId + "&page=1" ;
//            String html1 = Tool.getRequest(url1,"","", "", cookie);
//            Document doc1 = Jsoup.parse(html1);
//            Element table1= doc1.selectFirst("table.table.table-bordered");
//            if (table1 != null) {
//                Elements headers = table1.select("thead th");
//                headerList = new ArrayList<>();
//                for (Element header : headers) {
//                    headerList.add(header.text());
//                }
//                //处理表头和数据行
//                Elements rows1 = table1.select("tbody tr");
//                for (Element row : rows1) {
//                    Elements cells = row.select("td");
//                    Map<String, Object> rowData = new HashMap<>();
//                    for (int i = 0; i < cells.size() && i < headerList.size(); i++) {
//                        String headerText = headerList.get(i);
//                        String cellText = cells.get(i).text();
//                        rowData.put(headerText, cellText);
//                    }
//                    result.add(rowData);
//                    //从最后一页获取总期数
//                    String zuqi = rowData.get("期数").toString();
//                    String term = zuqi.split("/")[1];
//                    System.out.println("总期数："+term);
//                    //判断总期数是否大于15,大于15有第二页
//                    if (Integer.parseInt(term)>15){
//                        System.out.println("总期数："+term+"大于15 ，有第二页");
//                        String url2 = "https://admin.rrzu.com/v2-bill/index?order_id="+orderId + "&page=2" ;
//                        String html2 = Tool.getRequest(url2,"","", "", cookie);
//                        Document doc2 = Jsoup.parse(html2);
//                        Element table2= doc2.selectFirst("table.table.table-bordered");
//                        if (table2 != null) {
//                            Elements rows2 = table2.select("tbody tr");
//                            for (Element row2 : rows2) {
//                                Elements cells2 = row2.select("td");
//                                Map<String, Object> rowData2 = new HashMap<>();
//                                for (int i = 0; i < cells2.size() && i < headerList.size(); i++) {
//                                    String headerText = headerList.get(i);
//                                    String cellText = cells2.get(i).text();
//                                    rowData2.put(headerText, cellText);
//                                }
//                                result.add(rowData2);
//                            }
//                            System.out.println("成功获取到了第二页");
//                        }
//                    }
//                }
//            } else {
//                System.out.println("未找到表格");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return result;
//    }
    //获取指定页数据并添加到结果列表中
    public static List<Map<String, Object>> getRenRZuBillList(String authorization,String orderId,String  cookie) {
        List<Map<String, Object>> result = new ArrayList<>();
        List<String> firstHeaderList = new ArrayList<>(); //保存第一页的表头
        try {
            //获取第一页数据
            firstHeaderList = getPageData(orderId, 1, cookie, result, null);
            //System.out.println("成功获取到了第一页 ~ "+firstHeaderList);
            //获取总期数
            Map<String, Object> firstRow = result.get(0);
            String zuqi = firstRow.get("期数").toString();
            String cleanZuqi = zuqi.replaceAll("[^0-9/]", "");
            int totalTrems = Integer.parseInt(cleanZuqi.split("/")[1]);
            System.out.println("总期数："+totalTrems);
            if (totalTrems > 30){
                getPageData(orderId, 2, cookie, result,firstHeaderList);
                getPageData(orderId, 3, cookie, result,firstHeaderList);
                System.out.println("总期数："+totalTrems+"大于30 ，有第三页");
            }else if (totalTrems > 15){
                getPageData(orderId, 2, cookie, result,firstHeaderList);
                //System.out.println("第2页"+getPageData(orderId, 2, cookie, result,firstHeaderList));
                System.out.println("总期数："+totalTrems+"大于15 ，有第二页");
            }else {
                System.out.println("获取第一页数据");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    private static List<String> getPageData(String orderId, int page, String cookie, List<Map<String, Object>> result, List<String> firstHeaderList) {
        List<String> headerList = new ArrayList<>();
        try {
            String url = "https://admin.rrzu.com/v2-bill/index?order_id="+orderId + "&page="+page;
            String html = Tool.getRequest(url,"","", "", cookie);
            Document doc = Jsoup.parse(html);
            Element table = doc.selectFirst("table.table.table-bordered");
            if (table != null) {
                //获取当前实际表头(只在第一页获取)
                Elements headers = table.select("thead th");
                if (firstHeaderList == null){
                    for (Element header : headers) {
                        headerList.add(header.text());
                    }
                }else {
                    headerList = new ArrayList<>(firstHeaderList);
                }
                //处理表头和数据行
                Elements rows = table.select("tbody tr");
                for (Element row : rows) {
                    Elements cells = row.select("td");
                    Map<String, Object> rowData = new HashMap<>();
                    for (int i = 0; i < cells.size() && i < headerList.size(); i++) {
                        String headerText = headerList.get(i);
                        String cellText = cells.get(i).text();
                        rowData.put(headerText, cellText);
                    }
                    result.add(rowData);
                }
            }
            System.out.println("成功获取了第"+page+"页数据");
            return headerList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return headerList;
    }
}